package cn.myzqu.sign.project.server.utils;


import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * @Author: 的川
 * @Date: 2019/1/25 9:20
 */
public class BeanUtil {


    /**
     * 复制集合中对象的属性
     * @param source
     * @param targetClass
     * @param <T>
     * @return
     * @throws Exception
     */
    public static  <T> List<T> copyProperties( Collection<?> source , Class<?> targetClass ) throws Exception{
        List list = new ArrayList();
        if(source !=null){
            Iterator<?> it = source.iterator();
            while (it.hasNext()){
                Object t = it.next();
                //动态new目标类的对象
                Object object = targetClass.newInstance();
                BeanUtils.copyProperties(t,object);
                list.add(object);
            }
        }
        return list;
    }

    /**
     * 复制list的对象的属性
     * @param source
     * @param target
     * @return
     */
    public static  <T> List<T>  copyProperties( Collection<?> source , Object target ) {
        List list = new ArrayList();
        if(source !=null){
            Iterator<?> it = source.iterator();
            while (it.hasNext()){
                Object t = it.next();
                BeanUtils.copyProperties(t,target);
                list.add(target);
            }
        }
        return list;
    }


}
